// pages/login/login.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    src: app.data.imgurl +"logo.png",
    username:app.data.imgurl +"username.png",
    password:app.data.imgurl +"password.png",
    loginbag: app.data.imgurl +"loginbg.png" ,
    sign_inurl: app.data.url +'sheyingjia/myuser/xsign_in',
    shuju:{},
    xianshi:false,
    code:'',
  },


  wangjimima: function () {
    wx.navigateTo({
      url: '/pages/forget/forget',
    })
  },

  rigister: function () {
    wx.navigateTo({
      url: '/pages/reg/reg',
    })
  },
  bindKeyInput:function(e){
// console.log(e);
    var field = e.currentTarget.dataset.type;
    var str = e.detail.value;
    var shuju = this.data.shuju;
    shuju[field] = str;
    this.setData({shuju:shuju});
  },
  //登录接口
  login:function(){
    var shuju = this.data.shuju;
    var that = this;
    if(!shuju.phone){
      wx.showToast({
        title: '请填写手机',
      })
      return;
    }
    if (!shuju.pwd) {
      wx.showToast({
        title: '请填密码',
      })
      return;
    }
    wx.showLoading({
      title: '登陆中',
    })
    wx.request({
      url: that.data.sign_inurl,
      data:shuju,
      method:"POST",
      success:function(res){
        wx.hideLoading();
        var data = res.data;
        console.log(data);
        if (data.code==200){
          wx.setStorageSync('user', data.data);
          wx.showToast({
            title: data.msg,
          })
          
          setTimeout(function(){
            wx.switchTab({
              url: '/pages/my/my',
            })
          },app.data.times);
        }else{
          wx.showToast({
            title: data.msg,
            icon:'none'
          })
        }
      }
    })

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var user = wx.getStorageSync('user');
    console.log(user);
    var that = this;
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']){
          that.setData({ xianshi: false });
        }else{
          that.setData({ xianshi:true});
          wx.login({
            success: function (res) {
              console.log(res.code);
              var shuju = that.data.shuju;
              shuju.code = res.code ;
              that.setData({ shuju: shuju });
            }
          })
        }
      }
    })
  },
  //授权
  bindGetUserInfo:function(e){
    console.log(e);
    var that = this;
    if (e.detail.errMsg =='getUserInfo:ok'){
      wx.login({
        success:function(res){
          var shuju = that.data.shuju;
          shuju.code = res.code;
          that.setData({ shuju: shuju, xianshi:true});
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})